package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletScanCardContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletScanCardPresenter;
import com.meitianhui.convenitenbuy.util.RxTimerUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.zxing.activity.CaptureActivity;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/15.
 */

public class WalletScanCardActivity extends CaptureActivity implements WalletScanCardContract.View {

    private TextView mTvStatus;
    private RxTimerUtils mRx;
    private RxTimerUtils.IRxNext failNext;
    private Button mBtn;
    @Inject
    WalletScanCardPresenter presenter;

    @Override
    protected void initId() {
        super.initId();
        mTvStatus = getViewById(R.id.tv_wallet_scanning_card_status);
        setTitle(getString(R.string.wallet_cards_title));
    }

    @Override
    protected int setView() {
        return R.layout.wallet_card_container;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        mRx = new RxTimerUtils();
        failNext = new RxTimerUtils.IRxNext() {
            @Override
            public void doNext(long number) {
                mTvStatus.setText("");
                setReCapture();
            }
        };
    }

    @Override
    protected void receiveResult(String resultString) {
        if (TextUtils.isEmpty(resultString)) {
            mTvStatus.setText(getString(R.string.wallet_scanning_fail));
            mRx.timer(8000, failNext);
            return;
        }

        mTvStatus.setText(getString(R.string.wallet_scanning_waiting));
        presenter.verifyCard(resultString);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRx.cancel();
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void verifyCardBack(boolean result, String msg) {
        if (result) {
            Intent intent = new Intent(this, WalletAddCardActivity.class);
            intent.putExtra(Constants.ARGUMENT, msg);
            startActivity(intent);
        } else {
            TipUtils.showToast(App.getInstance(), msg);
        }
        finish();
    }
}
